import { _decorator, color, Component, find, Label, Node, Sprite, SpriteFrame } from 'cc';
import { Equipment } from '../Item/Equipment';
import { Core } from '../Core/Core';
import { EItemType, ItemQualityColors } from '../Item/Item';
import { EEvent, EView } from '../Config/Enum';
import { EPassiveSkill } from '../Config/SkillConfig';
import { ResourceManager } from '../Core/ResourceManager';
const { ccclass, property } = _decorator;


//item格子
@ccclass('ItemBox')
export class ItemBox extends Component {
    type: any

    //ndEquipmentDetail: Node | null = null;
    spBg: Sprite | null = null;

    lbDesc: Label | null = null;
    ndMask: Node | null = null;
    spIcon:Sprite
    





    equipment: Equipment

    protected onLoad(): void {

        console.log("ItemBox onLoad: this.node.name", this.node.name);

        this.lbDesc = this.node.getChildByName("Desc").getComponent(Label);
        this.ndMask = this.node.getChildByName("Mask");

        this.spIcon = this.node.getChildByName("Icon").getComponent(Sprite)

        
        



    }

    protected onEnable(): void {
        this.node.on(Node.EventType.TOUCH_END, this.onButtonChooseThis, this);


        
    }

    protected onDisable(): void {
        this.node.off(Node.EventType.TOUCH_END, this.onButtonChooseThis, this);
        
    }

    start() {

    }

    update(deltaTime: number) {
        
    }

    loadData() {

        
    }

    setDesc(desc: string) {
        if (this.lbDesc) {
            this.lbDesc.string = desc;
        }
    }
    setIcon(iconPath:String){

        ResourceManager.Instance.loadRes<SpriteFrame>(iconPath+"/spriteFrame").then((spriteFrame) => {
            this.spIcon.spriteFrame = spriteFrame;
        }).catch((error) => {
            console.error("加载图标失败:", error);
        });



    }


    setMask(levelLimit){
        if(Core.Instance.players.get(Core.Instance.currentPlayer).level >= levelLimit){
            this.ndMask.active = false
        }else{
            this.ndMask.active = true
        }


    }

    disableMask(){
        this.ndMask.active = false;
    }



    removeMask() {
        if (this.ndMask) {
            this.ndMask.active = false;
        }
    }

    onButtonChooseThis() {
        console.log("选择了：", this.node.name);

        if(Core.Instance.currentView === EView.MartialView){
            Core.Instance.event.emit(EEvent.FarmingUpdate, this.node.name);
        } else if(Core.Instance.currentView === EView.PlayerView){
            Core.Instance.event.emit(EEvent.PassiveSkillUpdate, this.node.name);
            //Core.Instance.players.get(Core.Instance.currentPlayer).skillData.assignedPassiveSkill = this.node.name as EPassiveSkill;

            //console.log("321ItemBox onButtonChooseThis assignedPassiveSkill:", Core.Instance.players.get(Core.Instance.currentPlayer).skillData.assignedPassiveSkill);
            //Core.Instance.event.emit(EEvent.PassiveSkillUpdate, this.node.name);
        }









        this.node.parent.parent.active = false;

    }

    onButtonReturnName(): string {
        return this.node.name;
    }




}


